package baseclass.d_list;


import utils.ListNode;

/**
 * 删除重复的节点1：{1,2,2,3,4,4,5} ->{1,2,3,4,5}
 * @date 2020/2/28 22:39
 */
public class Code11_DeleteRepeatNode {
    public static void deleteRepeat(ListNode head){
        if(head == null || head.next == null) return;
        ListNode pre = head;
        ListNode after = head.next;
        while (after != null){
            ListNode next = after.next;
            //删除after
            if(pre.val == after.val){
                pre.next = next;
                after.next = null;
            }else {
                //更新pre
                pre = after;
            }
            after = next;
        }
    }

    public static void main(String[] args) {
        ListNode pHead = new ListNode(1);
        pHead.next = new ListNode(1);
        pHead.next.next = new ListNode(2);
        pHead.next.next.next = new ListNode(3);
        pHead.next.next.next.next = new ListNode(3);
        pHead.next.next.next.next.next = new ListNode(4);
        pHead.next.next.next.next.next.next = new ListNode(5);
        pHead.next.next.next.next.next.next.next = new ListNode(5);
        deleteRepeat(pHead);
        while (pHead!=null){
            System.out.print(pHead.val + " ");
            pHead = pHead.next;
        }
    }

}
